package com.dubito.composenote

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.ui.Modifier
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.dubito.composenote.ui.screen.NoteAddScreen
import com.dubito.composenote.ui.screen.NoteDetailScreen
import com.dubito.composenote.ui.screen.NoteEditScreen
import com.dubito.composenote.ui.screen.NoteScreen
import com.dubito.composenote.ui.screen.NoteViewModel
import com.dubito.composenote.ui.screen.NoteViewModelFactory
import com.dubito.composenote.ui.theme.ComposeNoteTheme

class MainActivity : ComponentActivity() {

    lateinit var viewModel: NoteViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = NoteViewModelFactory(ComposeNoteApp.getDao()).create(NoteViewModel::class.java)
        setContent {
            ComposeNoteTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    val navController = rememberNavController()
                    NavHost(navController = navController, startDestination = Constants.NOTE_LIST) {
                        composable(route = Constants.NOTE_LIST) {
                            NoteScreen(navController = navController, viewModel = viewModel)
                        }
                        composable(
                            route = Constants.NOTE_DETAIL,
                            arguments = listOf(navArgument(Constants.NOTE_ID_ARGUMENT) {
                                type = NavType.IntType
                            })
                        ) { navBackStackEntry ->
                            navBackStackEntry.arguments?.getInt(Constants.NOTE_ID_ARGUMENT)?.let {
                                NoteDetailScreen(
                                    noteId = it,
                                    navController = navController,
                                    viewModel = viewModel
                                )
                            }
                        }
                        composable(
                            route = Constants.NOTE_Edit,
                            arguments = listOf(navArgument(Constants.NOTE_ID_ARGUMENT) {
                                type = NavType.IntType
                            })
                        ) { navBackStackEntry ->
                            navBackStackEntry.arguments?.getInt(Constants.NOTE_ID_ARGUMENT)?.let {
                                NoteEditScreen(
                                    noteId = it,
                                    navController = navController,
                                    viewModel = viewModel
                                )
                            }
                        }
                        composable(Constants.NOTE_CREATE) {
                            NoteAddScreen(navController = navController, viewModel = viewModel)
                        }
                    }
                }
            }
        }
    }
}
